package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.ScenicQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IScenicRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.domain.Comments;
import cn.wolfcode.luowowo.common.domain.ScenicComment;
import cn.wolfcode.luowowo.common.query.ScenicCommentObject;
import cn.wolfcode.luowowo.article.service.IScenicCommentMysqlService;
import cn.wolfcode.luowowo.common.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.webSite.annotation.UserParam;
import cn.wolfcode.luowowo.webSite.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IScenicService scenicService;

    @Reference
    private IScenicDetailService scenicDetailService;

    @Reference
    private IScenicTagService scenicTagService;

    @Reference
    private IScenicPicService scenicPicService;

    @Reference
    private IScenicCommentService scenicCommentService;

    @Reference
    private IScenicCommentMysqlService scenicCommentMysqlService;

    @Reference
    private IScenicRedisService scenicRedisService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")ScenicQuery qo, Model model){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //共享景点概况
        Scenic scenic = scenicService.get(qo);
        model.addAttribute("scenic",scenic);
        //必游景点TOP5
        List<ScenicDetail> top5 = scenicDetailService.getTop5(qo.getDestId());
        model.addAttribute("top5",top5);
        //热门景点TOP10
        List<ScenicDetail> hotscenics = scenicDetailService.getHotTop10(qo.getDestId());
        if(hotscenics.size() > 9){
            model.addAttribute("hot1",hotscenics.get(0));
            model.addAttribute("hot23",hotscenics.subList(1,3));
            model.addAttribute("hot45",hotscenics.subList(3,5));

            model.addAttribute("hot67",hotscenics.subList(5,7));
            model.addAttribute("hot8",hotscenics.get(7));
            model.addAttribute("hot0",hotscenics.subList(8,10));
        }

        //所有景点标签 tags
        List<ScenicTag> tags = scenicTagService.query();
        model.addAttribute("tags",tags);
        return "scenic/list";
    }


    @RequestMapping("/getall")
    public String getAll(@ModelAttribute("qo") ScenicQuery qo,Model model){
        PageInfo<ScenicDetail> pageInfo = scenicDetailService.getAll(qo);
//        System.out.println(qo.getTagId());
        model.addAttribute("pageInfo",pageInfo);
        return "scenic/scenicTpl";
    }


    @RequestMapping("/detail")
    public String detail(Model model,@ModelAttribute("qo") ScenicQuery qo,@UserParam UserInfo userInfo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        ScenicDetail scenicDetail = scenicDetailService.get(qo);
        model.addAttribute("scenicDetail",scenicDetail);

        List<ScenicPic> pics = scenicPicService.query(qo.getId());
        model.addAttribute("count",pics.size());
        model.addAttribute("pics",pics);
        model.addAttribute("user",userInfo);

        boolean ret = scenicRedisService.checkFavor(qo.getId(),userInfo.getId());
        model.addAttribute("ret",ret);
        return "scenic/detail";
    }

    @Value("${file.path}")
    private String filePath;

    //图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody  //插件要求必须返回Json
    public Object coverImageUpload(MultipartFile pic) {
        String filename = UploadUtil.upload(pic, filePath);
        return filename;
    }

    @RequestMapping("/saveComment")
    @ResponseBody
    public Object saveComment(@UserParam UserInfo userInfo, ScenicComment comment,Model model){
        //设置页面没有带过来的数据
        comment.setCity(userInfo.getCity());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());

        scenicCommentMysqlService.saveComment(comment);
        return AjaxResult.SUCCESS;
    }


    //评论显示
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") ScenicCommentObject qo,@UserParam UserInfo userInfo) {
        List<ScenicComment> all = scenicCommentMysqlService.query(qo);
//        List<ScenicComment> list = pageInfo.getList();
        List<ScenicComment> scs = new ArrayList<>();
        for (ScenicComment sc : all) {
            if(sc.getPicUrl()!=null){
                String picUrl = sc.getPicUrl();
                String[] pics1 = picUrl.split(",");
                List<String> newList = new ArrayList<>();
                for (String pic : pics1) {
                    if (StringUtils.hasLength(pic)) {
                        newList.add(pic);
                    }
                    sc.setPics(newList);
                }
                scs.add(sc);
            }
        }
        PageInfo<ScenicComment> pageInfo1 = new PageInfo<>(scs);
       /* ScenicComment sc = (ScenicComment) page.getContent().get(page.getContent().size() -1);
        String picUrl = sc.getPicUrl();
        String[] pics1 = picUrl.split(",");
        List<String> pics = new ArrayList<>();
        for (String pic : pics1) {
            if(StringUtils.hasLength(pic)){
                pics.add(pic);
            }
        }*/

        /*List<ScenicComment> list = page.getContent();
        List<ScenicComment> scs = new ArrayList<>();
        for (ScenicComment sc : list) {
            if(sc.getPicUrl()!=null){
                String picUrl = sc.getPicUrl();
                String[] pics1 = picUrl.split(",");
                List<String> newList = new ArrayList<>();
                for (String pic : pics1) {
                    if (StringUtils.hasLength(pic)) {
                        newList.add(pic);
                    }
                    sc.setPics(newList);
                }
                scs.add(sc);
            }
        }
        PageImpl page1 = new PageImpl(scs);
        model.addAttribute("page", page1);*/
//        model.addAttribute("pics",pics);
        Page<Comments> comments = scenicCommentService.getComments(qo);
        /*boolean ret = scenicRedisService.check(qo.getDetailId(),userInfo.getId());*/

        model.addAttribute("pageInfo",pageInfo1);
        model.addAttribute("comments",comments);
        model.addAttribute("user",userInfo);
        /*model.addAttribute("ret",ret);*/
        return "scenic/commentTpl";
    }

    @RequestMapping("/reply")
    @ResponseBody
    public Object reply(Comments comments, @UserParam UserInfo userInfo){
        if(userInfo == null){
            return AjaxResult.FAIL;
        }

        comments.setCity(userInfo.getCity());
        comments.setUserId(userInfo.getId());
        comments.setUsername(userInfo.getNickname());
        comments.setLevel(userInfo.getLevel());
        comments.setHeadUrl(userInfo.getHeadImgUrl());
        scenicCommentService.reply(comments);


        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/thumbup")
    @ResponseBody
    public Object thumbup(Long detailId,Long userId){
        boolean result = scenicRedisService.thumbup(detailId,userId);
        if(!result){
            return new AjaxResult(false,"你已经点赞过了");
        }

       boolean ret =  scenicCommentMysqlService.thumbup(detailId);
       int num =  scenicCommentMysqlService.getThumbup(detailId);

        if(ret){
            return AjaxResult.SUCCESS.addData(num);
        }
       return new AjaxResult(false,"点赞失败");
    }


    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long did, @UserParam UserInfo userInfo) {
        //登录判断
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);  //事先约定的未登录状态码
            return result;
        }
        boolean ret = scenicRedisService.ifFavor(did, userInfo.getId());

        return new AjaxResult(ret);
    }
}
